<template>
  <!-- 柱状图 -->
  <div
    class="chart-original"
    :class="className"
    :style="{ height: height, width: width }"
  />
</template>

<script>
import echarts from "echarts";
// require('echarts/theme/macarons') // echarts theme
import resize from "./mixins/resize";
import { resolve } from 'url';

export default {
  mixins: [resize],
  props: {
    name: {
      type: String,
      required: true,
    },
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
    xAxisName: {
      type: String,
    },
    tooltipName: {
      type: String,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        console.log(val, '~~~~~~~~~~')
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(this.chartData);
    },
    setOptions(expectedData) {
      let barWidth;
      if (expectedData.yAxisData.length >= 5) {
        barWidth = '50%';
      } else {
        barWidth = 15;
      }
      this.chart.clear();
      this.chart.setOption({
        toolbox: {
          show: false
        },
        // title: {
        //   text: "柱状图",
        //   left: "center",
        //   bottom: 0,
        // },
        xAxis: [
          {
            type: "value",
            name: this.xAxisName || "",
            nameLocation: "middle",
            nameGap: 25,
            nameRotate: 0,
            minInterval: 1,
            axisTick: {
              show: true,
              alignWithLabel: true,
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed",
              },
            },
          },
        ],
        yAxis: [
          {
            type: "category",
            name: this.name,
            nameLocation: "end",
            nameGap: 5,
            nameRotate: 0,
            axisTick: {
              show: true,
              alignWithLabel: true,
            },
            // splitLine: {
            //   show: true,
            //   lineStyle: {
            //     type: 'dashed'
            //   }
            // },
            data: expectedData.yAxisData,
            axisLabel: {
              formatter: function(value, index) {
                if (expectedData.yAnnotationAxisData && expectedData.yAnnotationAxisData.length != 0) {
                  return value + '\n' + expectedData.yAnnotationAxisData[index];
                } else {
                  return value;
                }       
              },
            }
          },
        ],
        grid: {
          left: 25, // 15
          right: 50,
          bottom: 35,
          top: 25,
          // top: '10%',
          // left: '2%',
          // right: '2%',
          // bottom: '2%',
          containLabel: true,
        },
        tooltip: { // 鼠标滑动的地方
          trigger: "axis",
          yAxisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "cross", // 默认为直线，可选为：'line' | 'shadow'
          },
          // padding: [5, 10]
          formatter: `{b}${this.tooltipName}：{c}`
        },
        legend: {
          right: 30,
          data: [
            {
              name: this.name,
              icon: "rect",
            },
          ],
          tooltip: {
            show: true,
          },
        },
        series: [
          {
            name: this.name,
            itemStyle: {
              color: "#40a9ff",
              borderWidth: 5,
            },
            type: "bar",
            stack: "vistors",
            barWidth: barWidth,
            // barMaxWidth: "50%",
            // barMinWidth: "50%",
            // barWidth: 10,
            data: expectedData.xAxisData,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
        ],
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.chart-original {
  margin: 0 16px;
}
</style>